/*
 * @Author: shiyue 2647472992@qq.com
 * @Date: 2024-08-25 16:46:58
 * @LastEditors: shiyue 2647472992@qq.com
 * @LastEditTime: 2024-08-25 17:09:17
 * @FilePath: \InsertSort\InsertSort.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

#include <iostream>
#include <vector>
#include <ctime>
using namespace std;

class InsertSort
{
    public:
    InsertSort(vector<int>& array)
    : _array(array)
	,_capacity(array.size())
    ,_size(array.size())
    {}

    ~InsertSort()
    {}

    void printarray()
    {
		int n = _size;
		for (int i = 0; i < n; ++i)
		{
				cout << _array[i] << " ";
		}
        cout << endl;
    }

    //升序排列
    void insertarray()
    {
        int n = _size;
        for (int i = 1; i < n; ++i)
        {
            int end  = i - 1;
            int tempt = _array[i];
            while (end >= 0)
            {
                if (tempt  < _array[end])
                {
					_array[end + 1] = _array[end]; 
                    --end;
                }
                else
                {
                    break;
                }
            }
            _array[end + 1] = tempt; 
        }
    }

    private:
    int _size;
    int _capacity;
    vector<int> _array;
};


int main()
{
    vector<int> arr(10, 8);
    srand(time(nullptr));
    for (auto&e : arr)
    {
        e =  rand() % 100;
    }
	InsertSort test(arr);
    test.printarray();
    test.insertarray();
    test.printarray();

    return 0;
}
